package ASTInterpreter;

import java.util.HashMap;

import stone.Symbols;

public class NestedEnv implements Environment {
	
	protected HashMap<String, Object> values;
	
	protected Environment outer;
	
	public NestedEnv() {
		this(null);
	}
	
	public NestedEnv(Environment env) {
		values = new HashMap<String, Object>();
		outer = env;
	}
	
	public void setOuter(Environment outer) {
		this.outer = outer;
	}

	@Override
	public void put(String name, Object value) {
		Environment env = where(name);
		if (env == null) {
			env = this;
		}
		env.putNew(name, value);
	}
	
	public void putNew(String name, Object value) {
		values.put(name, value);
	}
	
	public Environment where(String name) {
		if (values.get(name) != null) {
			return this;
		}
		else if (outer == null) {
			return null;
		}
		else {
			return (outer).where(name);
		}
	}

	@Override
	public Object get(String name) {
		Object v = values.get(name);
		if (v == null && outer != null) {
			return outer.get(name);
		}
		else {
			return v;
		}
	}

	@Override
	public Symbols symbols() {
		// TODO Auto-generated method stub
		return null;
	}

}
